package tutor;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.util.glu.GLU.*;
import static org.lwjgl.input.Keyboard.*;


import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.util.vector.Vector3f;

public class Tutorial8 extends Game {
    
    ShaderProgram shaderProgram;
    
    Camera camera;
    
    Model plane;
    
    /**
     * Initialize resources
     */
    public void init() {
    	
    	Display.setTitle("3d test 4");
    	
    	camera = new Camera(45, (float) Display.getWidth() / (float) Display.getHeight(), 0.1f, 100f);
    	camera.setPosition(new Vector3f(6, 0, 8));
    	camera.setRotation(0, 135, 0);
    	
    	//plane = Model.loadOBJModel("tutor/AirHawk.obj"); //BRING IT BACK
    	plane = Model.loadOBJModel("tutor/AirHawk.obj", 0, 0, 0);
    	
        
        // Create shader program
        shaderProgram = new ShaderProgram();
        shaderProgram.attachVertexShader("tutor/shader.vert");
        shaderProgram.attachFragmentShader("tutor/shader.frag");
        shaderProgram.link();
        
        glCullFace(GL_FRONT_FACE);
    }
    
    /**
     * Update logic
     */
    public void update(long elapsedTime)
    {
        if (isKeyDown(Keyboard.KEY_ESCAPE))
            end();

        // Look up
        if (isKeyDown(KEY_UP))
            camera.addRotation(-1f, 0, 0);

        // Look down
        if (isKeyDown(KEY_DOWN))
            camera.addRotation(1f, 0, 0);

        // Turn left
        if (isKeyDown(KEY_LEFT))
            camera.addRotation(0, -1f, 0);

        // Turn right
        if (isKeyDown(KEY_RIGHT))
            camera.addRotation(0, 1f, 0);

        // Move front
        if (isKeyDown(KEY_W))
            camera.move(0.1f, 1);

        // Move back
        if (isKeyDown(KEY_S))
            camera.move(-0.1f, 1);

        // Strafe left
        if (isKeyDown(KEY_A))
            camera.move(0.1f, 0);

        // Strafe right
        if (isKeyDown(KEY_D))
            camera.move(-0.1f, 0);
    }
    
    /**
     * Render to the screen
     */
	public void render() {
		// Clear the screen
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		// Perform camera transformations
		camera.apply();

		// Bind the shaders
		shaderProgram.bind();

		// Set the uniforms
		shaderProgram.setUniform("projection", camera.getProjectionMatrix());
		shaderProgram.setUniform("view", camera.getViewMatrix());
		shaderProgram.setUniform("lightPos", camera.getPosition());

		// Normal matrix
		shaderProgram.setUniform("mNormal", camera.getNormalMatrix());

		// Render the model
		plane.render();

		// Unbind the shaders
		ShaderProgram.unbind();
	}
    
    /**
     * Display resized
     */
	public void resized() {
		glViewport(0, 0, Display.getWidth(), Display.getHeight());
	}

	/**
	 * Dispose resources
	 */
	public void dispose() {
		plane.dispose();
		shaderProgram.dispose();
	}

	public static void main(String[] args) {
		new Tutorial8();
	}
}
